import router from './router'
import store from './store'
import auth from "./util/auth";
import util from "./util/util";

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
    if (auth.getToken()) {
        store.dispatch("defendUserInfor")
        store.dispatch("defendMenuList")

        auth.setToken(auth.getToken())
        if (to.path != "/login"&&to.path!="/error") {
            let path = store.state.pathList
            if (path == "" || path == null)
                path = util.decrypt(window.sessionStorage.getItem("pathList"))
            if(path!=""&&(path.indexOf(to.path)==-1))
                next("/error")
            else {
                next()
            }
        } else {
            next()
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next(`/login?redirect=${to.path}`)
        }
    }
})


